home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacGofer 0.22d / MacGofer 0.22d Release / Gofer Documentation / Gofer Manual / appx_b < prev    next >
Text File  |  1991-12-30  |  27KB  |  306 lines

  1. -------------------------------------------------------
  2.  
  3. (&&), (||)     :: Bool -> Bool -> Bool
  4. False && x      = False
  5. True  && x      = x
  6.  
  7. False || x      = x
  8. True  || x      = True
  9.  
  10. not            :: Bool -> Bool
  11. not True        = False
  12. not False       = True
  13.  
  14. and, or        :: [Bool] -> Bool
  15. and             = foldr (&&) True
  16. or              = foldr (||) False
  17.  
  18. any, all       :: (a -> Bool) -> [a] -> Bool
  19. any p           = or  . map p
  20. all p           = and . map p
  21.  
  22. otherwise      :: Bool
  23. otherwise       = True
  24.  
  25. -- Character functions: -----------------------------------------------------
  26.  
  27. primitive ord "primCharToInt" :: Char -> Int
  28. primitive chr "primIntToChar" :: Int -> Char
  29.  
  30.  
  31. isAscii, isControl, isPrint, isSpace            :: Char -> Bool
  32. isUpper, isLower, isAlpha, isDigit, isAlphanum  :: Char -> Bool
  33.  
  34. isAscii c     =  ord c < 128
  35.  
  36. isControl c   =  c < ' '    ||  c == '\DEL'
  37.  
  38. isPrint c     =  c >= ' '   &&  c <= '~'
  39.  
  40. isSpace c     =  c == ' '   || c == '\t'  || c == '\n'  || c == '\r'  ||
  41.                                c == '\f'  || c == '\v'
  42.  
  43. isUpper c     =  c >= 'A'   &&  c <= 'Z'
  44. isLower c     =  c >= 'a'   &&  c <= 'z'
  45.  
  46.  
  47.                                       98
  48.  
  49.  
  50.  
  51.  
  52. Introduction to Gofer         APPENDIX B: CONTENTS OF STANDARD PRELUDE          
  53.  
  54.  
  55. isAlpha c     =  isUpper c  ||  isLower c
  56. isDigit c     =  c >= '0'   &&  c <= '9'
  57. isAlphanum c  =  isAlpha c  ||  isDigit c
  58.  
  59.  
  60. toUpper, toLower      :: Char -> Char
  61.  
  62. toUpper c | isLower c  = chr (ord c - ord 'a' + ord 'A')
  63.           | otherwise  = c
  64.  
  65. toLower c | isUpper c  = chr (ord c - ord 'A' + ord 'a')
  66.           | otherwise  = c
  67.  
  68. -- Standard type classes: ---------------------------------------------------
  69.  
  70. class Eq a where
  71.     (==), (/=) :: a -> a -> Bool
  72.     x /= y      = not (x == y)
  73.  
  74. class Eq a => Ord a where
  75.     (<), (<=), (>), (>=) :: a -> a -> Bool
  76.     max, min             :: a -> a -> a
  77.  
  78.     x <  y            = x <= y && x /= y
  79.     x >= y            = y <= x
  80.     x >  y            = y < x
  81.  
  82.     max x y | x >= y  = x
  83.             | y >= x  = y
  84.     min x y | x <= y  = x
  85.             | y <= x  = y
  86.  
  87. class Ord a => Ix a where
  88.     range   :: (a,a) -> [a]
  89.     index   :: (a,a) -> a -> Int
  90.     inRange :: (a,a) -> a -> Bool
  91.  
  92. class Ord a => Enum a where
  93.     enumFrom       :: a -> [a]              -- [n..]
  94.     enumFromThen   :: a -> a -> [a]         -- [n,m..]
  95.     enumFromTo     :: a -> a -> [a]         -- [n..m]
  96.     enumFromThenTo :: a -> a -> a -> [a]    -- [n,n'..m]
  97.  
  98.     enumFromTo n m        = takeWhile (m>=) (enumFrom n)
  99.     enumFromThenTo n n' m = takeWhile ((if n'>=n then (>=) else (<=)) m)
  100.                                       (enumFromThen n n')
  101.  
  102. class Eq a => Num a where               -- simplified numeric class
  103.     (+), (-), (*), (/) :: a -> a -> a
  104.     negate             :: a -> a
  105.     fromInteger           :: Int -> a
  106.  
  107. -- Type class instances: ----------------------------------------------------
  108.  
  109. primitive primEqInt    "primEqInt",
  110.       primLeInt    "primLeInt"   :: Int -> Int -> Bool
  111.  
  112.  
  113.                                       99
  114.  
  115.  
  116.  
  117.  
  118. Introduction to Gofer         APPENDIX B: CONTENTS OF STANDARD PRELUDE          
  119.  
  120.  
  121. primitive primPlusInt  "primPlusInt",
  122.       primMinusInt "primMinusInt",
  123.       primDivInt   "primDivInt",
  124.       primMulInt   "primMulInt"  :: Int -> Int -> Int
  125. primitive primNegInt   "primNegInt"  :: Int -> Int
  126.  
  127. instance Eq Int  where (==) = primEqInt
  128.  
  129. instance Ord Int where (<=) = primLeInt
  130.  
  131. instance Ix Int where
  132.     range (m,n)      = [m..n]
  133.     index (m,n) i    = i - m
  134.     inRange (m,n) i  = m <= i && i <= n
  135.  
  136. instance Enum Int where
  137.     enumFrom n       = iterate (1+) n
  138.     enumFromThen n m = iterate ((m-n)+) n
  139.  
  140. instance Num Int where
  141.     (+)           = primPlusInt
  142.     (-)           = primMinusInt
  143.     (*)           = primMulInt
  144.     (/)           = primDivInt
  145.     negate        = primNegInt
  146.     fromInteger x = x
  147.  
  148. primitive primEqFloat    "primEqFloat",
  149.           primLeFloat    "primLeFloat"    :: Float -> Float -> Bool
  150. primitive primPlusFloat  "primPlusFloat", 
  151.           primMinusFloat "primMinusFloat", 
  152.           primDivFloat   "primDivFloat",
  153.           primMulFloat   "primMulFloat"   :: Float -> Float -> Float 
  154. primitive primNegFloat   "primNegFloat"   :: Float -> Float
  155. primitive primIntToFloat "primIntToFloat" :: Int -> Float
  156.  
  157. instance Eq Float where (==) = primEqFloat
  158.  
  159. instance Ord Float where (<=) = primLeFloat
  160.  
  161. instance Enum Float where
  162.     enumFrom n       = iterate (1.0+) n
  163.     enumFromThen n m = iterate ((m-n)+) n
  164.  
  165. instance Num Float where
  166.     (+)         = primPlusFloat
  167.     (-)         = primMinusFloat
  168.     (*)         = primMulFloat
  169.     (/)         = primDivFloat 
  170.     negate      = primNegFloat
  171.     fromInteger = primIntToFloat
  172.  
  173. instance Eq Char where c == d  =  ord c == ord d
  174.  
  175. instance Ord Char where c <= d  =  ord c <= ord d
  176.  
  177.  
  178.  
  179.                                       100
  180.  
  181.  
  182.  
  183.  
  184. Introduction to Gofer         APPENDIX B: CONTENTS OF STANDARD PRELUDE          
  185.  
  186.  
  187. instance Ix Char where
  188.     range (c,c')      = [c..c']
  189.     index (c,c') ci   = ord ci - ord c
  190.     inRange (c,c') ci = ord c <= i && i <= ord c' where i = ord ci
  191.  
  192. instance Enum Char where
  193.     enumFrom c        = map chr [ord c ..]
  194.     enumFromThen c c' = map chr [ord c, ord c' ..]
  195.  
  196. instance Eq a => Eq [a] where
  197.     []     == []     =  True
  198.     []     == (y:ys) =  False
  199.     (x:xs) == []     =  False
  200.     (x:xs) == (y:ys) =  x==y && xs==ys
  201.  
  202. instance Ord a => Ord [a] where
  203.     []     <= _      =  True
  204.     (_:_)  <= []     =  False
  205.     (x:xs) <= (y:ys) =  x<y || (x==y && xs<=ys)
  206.  
  207. instance (Eq a, Eq b) => Eq (a,b) where
  208.     (x,y) == (u,v)  =  x==u && y==v
  209.  
  210. instance Eq Bool where
  211.     True  == True   =  True
  212.     False == False  =  True
  213.     _     == _      =  False
  214.  
  215. -- Standard numerical functions: --------------------------------------------
  216.  
  217. primitive div    "primDivInt",
  218.           rem    "primRemInt",
  219.           mod    "primModInt"    :: Int -> Int -> Int
  220.  
  221. subtract  :: Num a => a -> a -> a
  222. subtract   = flip (-)
  223.  
  224. even, odd :: Int -> Bool
  225. even x     = x `rem` 2 == 0
  226. odd        = not . even
  227.  
  228. gcd       :: Int -> Int -> Int
  229. gcd x y    = gcd' (abs x) (abs y)
  230.              where gcd' x 0 = x
  231.                    gcd' x y = gcd' y (x `rem` y)
  232.  
  233. lcm       :: Int -> Int -> Int
  234. lcm _ 0    = 0
  235. lcm 0 _    = 0
  236. lcm x y    = abs ((x `div` gcd x y) * y)
  237.  
  238. (^)       :: Int -> Int -> Int
  239. x ^ 0      = 1
  240. x ^ (n+1)  = f x n x
  241.              where f _ 0 y = y
  242.                    f x n y = g x n where
  243.  
  244.  
  245.                                       101
  246.  
  247.  
  248.  
  249.  
  250. Introduction to Gofer         APPENDIX B: CONTENTS OF STANDARD PRELUDE          
  251.  
  252.  
  253.                              g x n | even n    = g (x*x) (n`div`2)
  254.                                    | otherwise = f x (n-1) (x*y)
  255.  
  256. abs :: Int -> Int
  257. abs x    | x >= 0  = x
  258.          | x <  0  = - x
  259.  
  260. signum :: Int -> Int
  261. signum x | x == 0  = 0
  262.          | x > 0   = 1
  263.          | x < 0   = -1
  264.  
  265. sum, product    :: [Int] -> Int
  266. sum              = foldl' (+) 0
  267. product          = foldl' (*) 1
  268.  
  269. sums, products    :: [Int] -> [Int]
  270. sums             = scanl (+) 0
  271. products         = scanl (*) 1
  272.  
  273. -- Standard list processing functions: --------------------------------------
  274.  
  275. head             :: [a] -> a
  276. head (x:_)        = x
  277.  
  278. last             :: [a] -> a
  279. last [x]          = x
  280. last (_:xs)       = last xs
  281.  
  282. tail             :: [a] -> [a]
  283. tail (_:xs)       = xs
  284.  
  285. init             :: [a] -> [a]
  286. init [x]          = [x]
  287. init (x:xs)       = x : init xs
  288.  
  289. (++)             :: [a] -> [a] -> [a]    -- append lists.  Associative with
  290. []     ++ ys      = ys                   -- left and right identity [].
  291. (x:xs) ++ ys      = x:(xs++ys)
  292.  
  293. length         :: [a] -> Int           -- calculate length of list
  294. length            = foldl' (\n _ -> n+1) 0
  295.  
  296. (!!)             :: [a] -> Int -> a      -- xs!!n selects the nth element of
  297. (x:_)  !! 0       = x                    -- the list xs (first element xs!!0)
  298. (_:xs) !! (n+1)   = xs !! n              -- for any n < length xs.
  299.  
  300. iterate          :: (a -> a) -> a -> [a] -- generate the infinite list
  301. iterate f x       = x : iterate f (f x)  -- [x, f x, f (f x), ...
  302.  
  303. repeat           :: a -> [a]             -- generate the infinite list
  304. repeat x          = xs where xs = x:xs   -- [x, x, x, x, ...
  305.  
  306. cycle            :: [a] -> [a]           --